﻿using System;
using System.Collections.Generic;
using STEM123.StemService;

namespace STEM123
{
    /// <summary>
    /// Used by UI elements to maintain the state related to search view of application, i.e.
    /// slides being viewed, search results being presented and associated content.
    /// </summary>
    public class SearchViewModel : IViewModel
    {
        #region Private members

        /// <summary>
        /// Maximum slides allowed.
        /// </summary>
        private const uint MaximumSlides = 3;

        /// <summary>
        /// Initializes a new instance of SearchViewModel.
        /// </summary>
        private SearchViewModel()
        {
            Slides = new Slide[MaximumSlides];
            Slides[0] = new Slide();
            Slides[1] = new Slide();
            Slides[2] = new Slide();
        }

        #endregion

        #region Public members

        /// <summary>
        /// Static constructor.
        /// </summary>
        static SearchViewModel()
        {
            Instance = new SearchViewModel();
        }

        /// <summary>
        /// Instance of search view model.
        /// </summary>
        public static SearchViewModel Instance { get; private set; }

        /// <summary>
        /// Term being searched.
        /// </summary>
        public string Term { get; set; }

        /// <summary>
        /// Results of search.
        /// </summary>
        public SearchResults Results { get; set; }

        /// <summary>
        /// Slides currently being viewed.
        /// </summary>
        public Slide[] Slides { get; private set; }

        /// <summary>
        /// Selects a presentation for view.
        /// </summary>
        /// <param name="presentationId">Id of presentation.</param>
        public void SelectPresentation(uint presentationId)
        {
            Presentation[] presentations = this.Results.Presentations;
            
            for (int i = 0; i < presentations.Length; i++)
            {
                if (presentations[i].Id == presentationId)
                {
                    this.Slides = presentations[i].Slides;
                    break;
                }
            }
        }

        #endregion
    }

    /// <summary>
    /// Represents a search result item which can be displayed in the UI.
    /// </summary>
    public class SearchResultsItem
    {
        #region Private members

        /// <summary>
        /// Retreives icon / image corresponding to a media type.
        /// </summary>
        /// <param name="mediaType">Type of media.</param>
        /// <returns>Path of corresponding icon / image.</returns>
        private string getIconForMediaType(MediaTypes mediaType)
        {
            string iconMediaType = "Images/iconText.png";

            if (mediaType == MediaTypes.TextWithImage)
            {
                iconMediaType = "Images/iconImage.png";
            }
            else if (mediaType == MediaTypes.AudioOnly || mediaType == MediaTypes.AudioWithImage)
            {
                iconMediaType = "Images/speaker.png";
            }
            else if (mediaType == MediaTypes.Video)
            {
                iconMediaType = "Images/iconAV.png";
            }

            return iconMediaType;
        }

        #endregion

        #region Public members
        
        /// <summary>
        /// Id of presentation.
        /// </summary>
        public uint Id { get; set; }

        /// <summary>
        /// First question.
        /// </summary>
        public string Question1 { get; set; }

        /// <summary>
        /// Second question.
        /// </summary>
        public string Question2 { get; set; }

        /// <summary>
        /// Third question.
        /// </summary>
        public string Question3 { get; set; }

        /// <summary>
        /// Type of media used in first slide.
        /// </summary>
        public string MediaType1 { get; set; }

        /// <summary>
        /// Type of media used in second slide.
        /// </summary>
        public string MediaType2 { get; set; }

        /// <summary>
        /// Type of media used in third slide.
        /// </summary>
        public string MediaType3 { get; set; }

        /// <summary>
        /// Number of times the presentation was viewed.
        /// </summary>
        public uint TimesViewed { get; set; }

        /// <summary>
        /// Date and time when presentation was published.
        /// </summary>
        public DateTime PublishedOn { get; set; }

        /// <summary>
        /// Retrieves list of search result items from array of presentations.
        /// </summary>
        /// <param name="presentations">Array of presentations.</param>
        /// <returns>List of search result items.</returns>
        public static List<SearchResultsItem> GetSearchResults(Presentation[] presentations)
        {
            List<SearchResultsItem> results = new List<SearchResultsItem>();

            for (int i = 0; i < presentations.Length; i++)
            {
                results.Add(new SearchResultsItem(presentations[i]));
            }

            return results;
        }

        /// <summary>
        /// Creates a new instance of search result item from presentation.
        /// </summary>
        /// <param name="presentations">Instance of presentation.</param>
        public SearchResultsItem(Presentation presentations)
        {
            this.Id = presentations.Id;
            this.Question1 = presentations.Slides[0].Question;
            this.Question2 = presentations.Slides[1].Question;
            this.Question3 = presentations.Slides[2].Question;
            this.MediaType1 = getIconForMediaType(presentations.Slides[0].MediaType);
            this.MediaType2 = getIconForMediaType(presentations.Slides[1].MediaType);
            this.MediaType3 = getIconForMediaType(presentations.Slides[2].MediaType);
            this.TimesViewed = presentations.TimesViewed;
            this.PublishedOn = presentations.PublishedOn;
        }

        #endregion
    }
}